#ifndef __WORK_CMD__
#define __WORK_CMD__
#include "my_header.h"
#define TIME_SLICE 300
#define SATL_LEN 14
#define MD5_SIZE 16
#define MD5_STR_LEN (MD5_SIZE * 2)
typedef enum{
    REGISTER = 0,
    LOGIN,
    LS,
    CD,
    PWD,
    PUTS,
    GETS,
    REMOVE,
    MKDIR,
    RMDIR,
    TOKEN,
}commandType;

typedef struct TLVtrain_s{
    commandType type;
    int length;
    char data[4096];
}TLVtrain_t;

typedef struct train_s{
    int length;
    char data[4096];
}train_t;

typedef struct trainState_s{
    int length;
    int state;
    //注册登录，记录用户名长度
    //命令，记录部分命令长度
    int preLength;
    char buf[1024];
}trainState_t;

//环形队列
typedef struct slotNode_s{
    int netFd;
    struct slotNode_s* next;
}slotNode_t;

typedef struct slotList_s{
    slotNode_t * head;
    slotNode_t * tail;
    int size;
}slotList_t;

typedef struct clientQueue_s{
    //记录每个客户端fd
    int clientFd[50];
    //环形队列的长度，0.1s一次监听
    slotList_t timeOut[TIME_SLICE];
    //记录当前netfd所在位置
    int idex[50];
    //记录查询位置
    int timer;
}clientQueue_t;

//日志操作
int sysadd(int userId,char *cmd);

//客户端队列操作
int initClientQueue(clientQueue_t *pclientQueue);
int addFd(int netFd,clientQueue_t *pclientQueue);
int delFd(int netFd,clientQueue_t *pclientQueue);

//用户注册登录
int makeSalt(char *salt);
int registerUser(trainState_t trainState,int netFd);
int login(trainState_t trainState,int netFd,clientQueue_t *pclientQueue);
int token(trainState_t trainState,int netFd);
int encode(char *key,char *userName,char *token);
int decode(char *key,char *userName,char *token);

//处理命令
int handleShortCmd(trainState_t trainState,int netFd,int userId);
int recvn(int sockfd, void *buf, int length);
int handleLongCmd(int netFd,int userId);
int upload(int netFd,char * file, int userId);
int download(int sockFd,int userId);

//数据库操作
int mysqlConnect(MYSQL **pconn);
int doQuery(char *query,MYSQL **pconn);

//用户表操作
int getUserId(char *userName,int *puserId);
int getUserName(char *userName,int userId);
int getUserSalt(char *userName,char *salt, char *cryptPasswd);
//获取当前目录文件夹编号
int getUserCode(int userId,int *pcode);
int getUserPwd(int userId,char *pwd);
int updateUserPwd(int userId,char *pwd,int code);

//虚拟文件表操作
int initLocalFile();
// 增加链接数                                
int addLink(char *md5Str);
// 减少链接数
int minusLink(char *md5Str);
// 获取文件md5值，也可以检查文件是否存在，存在返回0，不存在返回1
int getVirtualFileMd5(char *file, int userId, int parentId, char *md5Str);
// 获取文件本地真实名称
int getRealFileName(char *md5Str, char *realFileName);
int getFileCode(int userId, char *fileName, int *pcode, int parentId, char *type);
int getParentId(int id, int *pparentCode);
// 在本地文件表中插入新数据
int addLocalFile(char *md5Str, char *realName);
int addVirtualDir(int parentId, char *fileName, int ownerId);
int addVirtualFile(int parentId, char *fileName, int ownerId, char *md5Str, int filesize);
// 删除虚拟文件/目录
int delVirtualFile(int code, int userId);
int ifLocalExist(char *md5Str);
//ls功能，将文件id为code的文件夹全部发给客户端
int findCodeFiles(int netFd, int code, int userId);





#endif
